package com.sfh.controller;

import java.io.File;

import com.jfinal.core.JFinal;
import com.jfinal.upload.UploadFile;
import com.sfh.base.BaseController;
import com.sfh.base.ReturnData;

public class SysController extends BaseController {
		
	// 图像上传
	public void upFile() {
		try {
			UploadFile uploadFile = this.getFile("file");

			if (uploadFile == null) {
				renderJson(ReturnData.error(101, "请求参数不完整"));
				return;
			}

			File file = uploadFile.getFile();
			if (file == null) {
				renderJson(ReturnData.error(202, "上传失败"));
				return;
			}
			this.renderJson(ReturnData.ok(file.getName()));
		} catch (Exception e) {
			System.out.println("文件保存："+e);
			this.renderJson(ReturnData.error(100, e.getMessage()));
		}
		
	}

	public void loadFile() {
		String path = getPara("image");
		// 用户设置的默认上传目录
		String saveDir = JFinal.me().getConstants().getBaseUploadPath();
	
		File file = new File(saveDir + File.separator + path);
		if (file.exists() && file.isFile()) {
			this.renderFile(file);
			return;
		}
		System.out.println("文件不存在："+file.getAbsolutePath());
		renderNull();
	}
}
